package com.kchouse.mine;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gxz.PagerSlidingTabStrip;
import com.kchouse.ListFragment;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.SharedPreferencesUtil;

import java.util.ArrayList;

public class OrderActivity extends BaseActivity implements View.OnClickListener{

    private OrderActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private PagerSlidingTabStrip pst;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    //声明pst的标题
    String[] titles = {"全部", "待收货","消费与评价"};
    private Intent intent;
    private String ordertype;
    private ListFragment list3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        mContent = this;
        intiData();
        intiView();


    }

    private void intiView() {
        tv_title=(TextView)findViewById(R.id.tv_title);
        tv_title.setText("我的订单");
        iv_back=(ImageView)findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        intiPSTS();

        switch (ordertype)
        {
            case "全部订单":
                viewPager.setCurrentItem(0);
                break;
            case "待收货":
                viewPager.setCurrentItem(1);
                break;
            case "消费与评价":
                viewPager.setCurrentItem(2);
                break;
        }
    }

    private void intiData() {

        intent=getIntent();
        ordertype=  intent.getStringExtra("ordertype");
    }

    /**
     * 初始化tab
     */
    private void intiPSTS() {

        pst = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.pager);
        ListFragment list1 = new ListFragment(2,"订单全部");//未使用优惠券
        ListFragment list2 = new ListFragment(4,"订单待收货");//使用优惠券
         list3 = new ListFragment(2,"消费与评价");//消费与评价
//        //添加fragment到集合中时注意顺序
        fragments.add(list1);
        fragments.add(list2);
        fragments.add(list3);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        pst.setViewPager(viewPager);
        viewPager.setCurrentItem(0);

        // 设置Tab是自动填充满屏幕的
        pst.setShouldExpand(true);
// 设置Tab的分割线的颜色
        pst.setDividerColor(getResources().getColor(R.color.colorred));
// 设置分割线的上下的间距,传入的是dp
        pst.setDividerPaddingTopBottom(10);
////设置Tab底部线的颜色
        pst.setUnderlineColor(getResources().getColor(R.color.bg_line));

// 设置Tab 指示器Indicator的高度,传入的是dp
        pst.setIndicatorHeight(3);
// 设置Tab Indicator的颜色
        pst.setIndicatorColor(getResources().getColor(R.color.colorred));

// 设置Tab标题文字的大小,传入的是sp
        pst.setTextSize(13);
// 设置选中Tab文字的颜色
        pst.setSelectedTextColor(getResources().getColor(R.color.colorred));
//设置正常Tab文字的颜色
        pst.setTextColor(getResources().getColor(R.color.colorblack));
//设置Tab文字的左右间距,传入的是dp
        pst.setTabPaddingLeftRight(12);

//是否支持动画渐变(颜色渐变和文字大小渐变)
        pst.setFadeEnabled(false);
// 设置最大缩放,是正常状态的0.3倍
//        pst.setZoomMax(0.3F);
        pst.setZoomMax(0);

//这是当点击tab时内容区域Viewpager是否是左右滑动,默认是true
        pst.setSmoothScrollWhenClickTab(true);
    }

    /**
     * * 自定义适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }//设置每页的标题

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        //设置每一页对应的fragment
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        //fragment的数量
        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                clearAct();
                finish();

                break;
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("EvaAction");
        //注册广播
        registerReceiver(new MyReceiver(), myIntentFilter);
    }

    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            list3.refresheva();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            clearAct();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
